package com.dalin.platform.glb.service.imple;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.dalin.common.pager.PageInfo;
import com.dalin.common.utils.DateUtils;
import com.dalin.common.utils.FileUtils;
import com.dalin.common.utils.search.LuceneUtils;
import com.dalin.framework.service.imple.FrameServiceImple;
import com.dalin.platform.glb.condition.WsGlbAttachmentCondition;
import com.dalin.platform.glb.model.GlbAttachmentEntity;
import com.dalin.platform.glb.model.GlbGeneratorKeyEntity;
import com.dalin.platform.glb.service.WsGlbService;
import com.dalin.platform.glb.vo.WsGlbAttachmentIn;
import com.dalin.platform.glb.vo.WsGlbAttachmentOut;
import com.dalin.platform.loa.LoaConst;

public class GlbServiceImple extends FrameServiceImple implements WsGlbService {

	public Long getNextGroupId() throws Exception {
		List<GlbGeneratorKeyEntity> entyList = getDao()
				.queryEqualObjs(GlbGeneratorKeyEntity.class, 
						"genKey", "NextGroupId");
		GlbGeneratorKeyEntity enty = entyList.get(0);
		enty.setGenValue(enty.getGenValue() + 1);
		getDao().updateObj(enty);
		return enty.getGenValue() - 1;
	}

	public PageInfo queryGlbAttachmentes(WsGlbAttachmentCondition cond)
			throws Exception {
		PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
		List<GlbAttachmentEntity> entyList = pageInfo.getDataList();
		List<WsGlbAttachmentOut> voList = new ArrayList<WsGlbAttachmentOut>();
		String rootDownloadPath = LoaConst
				.getFileInfoParam("rootDownloadPath");
		for (GlbAttachmentEntity enty : entyList) {
			WsGlbAttachmentOut v = new WsGlbAttachmentOut();
			v.setProjectId(enty.getProjectId());
			v.setAttachmentId(enty.getAttachmentId());
			v.setBizType(enty.getBizType());
			v.setBizDesc1(enty.getBizDesc1());
			v.setBizDesc2(enty.getBizDesc2());
			v.setBizDesc3(enty.getBizDesc3());
			v.setBizDesc4(enty.getBizDesc4());
			v.setAttachmentName(enty.getAttachmentName());
			v.setGroupId(enty.getGroupId());
			v.setCreateUserId(enty.getCreateUserId());
			v.setCreateUserName(enty.getCreateUserName());
			v.setAttachmentType(enty.getAttachmentType());
			v.setCreateDate(enty.getCreateDate());
			v.setRootDownloadPath(rootDownloadPath);
			v.setRelativePath(enty.getAttachmentSavedDir()
					+ enty.getAttachmentSavedName());
			voList.add(v);
		}
        pageInfo.setDataList(voList); 
		return pageInfo;
	}
	public List<WsGlbAttachmentOut> queryGlbAttachmentesForList(WsGlbAttachmentCondition cond)
		throws Exception {
		cond.setPageSize(Integer.MAX_VALUE);
		return queryGlbAttachmentes(cond).getDataList();
	}

	public WsGlbAttachmentOut findGlbAttachmentById(Long id) throws Exception {
		String rootDownloadPath = LoaConst
				.getFileInfoParam("rootDownloadPath");
		GlbAttachmentEntity enty = getDao().findObjById(GlbAttachmentEntity.class,id);
		return getWsGlbAttachmentOutFromEnty(enty);
	}

	public WsGlbAttachmentOut addGlbAttachment(WsGlbAttachmentIn vo) throws Exception {
		String fe = FileUtils.getFileExtention(vo.getAttachmentName()).toLowerCase();
		try {
			String rootPath = LoaConst.getFileInfoParam("rootUploadPath") ;
			String savedDir = vo.getProjectId() + "/" + fe + "/";
			String savedName = vo.getBizType()
					+ "_" + vo.getGroupId()
					+ "_"+ System.currentTimeMillis() // TODO: 此处应该用sequence或其他方式
					+ "." + fe;
			File destFile = new File(rootPath + savedDir + savedName);
			FileUtils.saveAsFile(vo.getDataFile(), destFile);
			
			GlbAttachmentEntity enty = new GlbAttachmentEntity();
			enty.setProjectId(vo.getProjectId());
			enty.setBizType(vo.getBizType());
			enty.setAttachmentSize(destFile.length() / 1024 + 1);
			enty.setAttachmentName(vo.getAttachmentName());
			enty.setBizDesc1(vo.getBizDesc1());
			enty.setBizDesc2(vo.getBizDesc2());
			enty.setBizDesc3(vo.getBizDesc3());
			enty.setBizDesc4(vo.getBizDesc4());
			enty.setAttachmentSavedDir(savedDir);
			enty.setAttachmentSavedName(savedName);
			enty.setCreateUserId(vo.getCreateUserId());
			enty.setCreateUserName(vo.getCreateUserName());
			enty.setAttachmentType(fe);
			enty.setCreateDate(DateUtils.getCurrentDate());
			enty.setGroupId(vo.getGroupId());
			getDao().createObj(enty);
			if (vo.isBuildIndex() && "doc".equals(enty.getAttachmentType())) {
				LuceneUtils.buildIndex(new File(LoaConst.getFileInfoParam("rootUploadPath") + savedDir), new File(
						LoaConst.getFileInfoParam("indexStorePath")));
			}
			return getWsGlbAttachmentOutFromEnty(enty);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void addGlbAttachmentes(List<WsGlbAttachmentIn> list) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void modifyGlbAttachment(WsGlbAttachmentOut vo) throws Exception {
		GlbAttachmentEntity enty = getDao().findObjById(GlbAttachmentEntity.class,
				vo.getAttachmentId());
		enty.setProjectId(vo.getProjectId());
		enty.setBizType(vo.getBizType());
		enty.setAttachmentName(vo.getAttachmentName());
		enty.setBizDesc1(vo.getBizDesc1());
		enty.setBizDesc2(vo.getBizDesc2());
		enty.setBizDesc3(vo.getBizDesc3());
		enty.setBizDesc4(vo.getBizDesc4());
		enty.setGroupId(vo.getGroupId());
	}

	public void modifyGlbAttachmentes(List<WsGlbAttachmentOut> list) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void delGlbAttachmentesByIds(Long[] idArray) throws Exception {
		getDao().deleteObjs(GlbAttachmentEntity.class, idArray);
		// TODO: 删除文件及索引，否则就就周期性同步数据库与文件
	}

	public void delGlbAttachmentesByGroupId(Long groupId) throws Exception {
		getDao().bulkUpdate("delete from GlbAttachmentEntity where groupId=" +groupId);
		// TODO: 删除文件及索引，否则就就周期性同步数据库与文件
	}

	private WsGlbAttachmentOut getWsGlbAttachmentOutFromEnty(GlbAttachmentEntity enty) {
		String rootDownloadPath = LoaConst
				.getFileInfoParam("rootDownloadPath");
		WsGlbAttachmentOut v = new WsGlbAttachmentOut();
		v.setProjectId(enty.getProjectId());
		v.setAttachmentId(enty.getAttachmentId());
		v.setBizType(enty.getBizType());
		v.setAttachmentName(enty.getAttachmentName());
		v.setBizDesc1(enty.getBizDesc1());
		v.setBizDesc2(enty.getBizDesc2());
		v.setBizDesc3(enty.getBizDesc3());
		v.setBizDesc4(enty.getBizDesc4());
		v.setGroupId(enty.getGroupId());
		v.setCreateUserId(enty.getCreateUserId());
		v.setCreateUserName(enty.getCreateUserName());
		v.setAttachmentType(enty.getAttachmentType());
		v.setCreateDate(enty.getCreateDate());
		v.setRootDownloadPath(rootDownloadPath);
		v.setRelativePath(enty.getAttachmentSavedDir()
				+ enty.getAttachmentSavedName());
		return v;
	}
}
